#添加物品信息
import sys
sys.path.append("..")
import json
from flask import request
from create_id import get_id
from db_client import *

def store_item():
    # 存储物品信息
    token = request.headers.get('token')
    flag, right = token_client.check_token(token)
    if not flag:
        t = {
            'state': 'false',
            'msg': '增加物品信息失败'
        }
        return json.dumps(t, ensure_ascii=False)
    else:
        _id = 'WP'
        num = mysql_client.get_item_row() + 1
        _id = _id + str(num)
        data = json.loads(request.get_data())
        item_desc = data['item_desc']
        item_type = data['type']
        item_name = data['item_name']

        flag2 = mysql_client.store_item(_id, item_desc, item_type, item_name)
        
        if flag2:
            t = {
                'state': 'true',
                'msg': 'success'
            }
            return json.dumps(t, ensure_ascii=False)
        else:
            t = {
                'state': 'false',
                'msg': '增加物品信息失败，稍后再试。'
            }
            return json.dumps(t, ensure_ascii=False)